<?php
class loginControl extends skymvc{
	
	public function __construct(){
		parent::__construct();
	}
	
	public function onDefault(){
		if(SHOPID){
			$this->gourl("/shopadmin.php?m=index");
		}
		$this->smarty->display("login/login.html");
	}
	
	public function onSave(){
		$adminname=post('adminname','h');
		$row=M("shopadmin")->selectRow(array("where"=>" adminname='".$adminname."' "));
		if($row && $row['password']==umd5(post('password').$row['salt'])){
			$_SESSION['ssshopadmin']=$row;
			$authcode=jiami($row['adminid']."|".umd5($row['password']));
			setcookie("shop_authcode",$authcode,time()+3600000,"/",DOMAIN);
			//处理推送相关
			
			M("apppush")->add("shopadmin",$row);
			$this->goall("登录成功",0,0,APPSHOP."?m=iframe");
		}else{
			$this->goall("账户或者密码出错",1);
		}
	}
	
	public function onlogout(){
		$_SESSION['ssshopadmin']="";
		setcookie("shop_authcode","",time()-3999,"/",DOMAIN);	
		$this->goall("退出成功",0,0,APPSHOP);
	}
	
	public function onuser(){
		if($_SESSION['ssshopadmin']) $this->gourl(APPSHOP."?m=index");
		$user=M("login")->getUser();
		if($user['shopid']){
			$shopadmin=M("shopadmin")->selectRow(array("where"=>" shopid=".$user['shopid']." "));
			if(empty($shopadmin)){
				$shopadmin=array(
					"shopid"=>$user['shopid'],
					"adminname"=>$shopid."_默认管理员",
					"password"=>md5(1234567)
				);
				M("shopadmin")->insert($shopadmin);
			}
			$_SESSION['ssshopadmin']=$shopadmin;
			$this->goall("登录成功",0,0,APPSHOP."?m=iframe"); 
		}else{
			$this->goall("你没有权限",1,0,"/index.php");
		}
	}
	
	public function onAdmin(){
		M("login")->checkAdminLogin();
		$shopid=get('shopid','i');
		$shop=M("shop")->selectRow("shopid=".$shopid);
		$shopadmin=M("shopadmin")->selectRow(array("where"=>" shopid=".$shopid." "));
			if(empty($shopadmin)){
				$shopadmin=array(
					"shopid"=>$shopid,
					"adminname"=>$shopid."_默认管理员",
					"password"=>md5(1234567),
					"siteid"=>$shop['siteid']
					
				);
				M("shopadmin")->insert($shopadmin);
			}
			$_SESSION['ssshopadmin']=$shopadmin;
			
			$this->gourl(APPSHOP."?m=iframe"); 
	}
		
}
?>